package wiki.zimo.certificate_manage.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import wiki.zimo.certificate_manage.dao.CertificateTypeDao;
import wiki.zimo.certificate_manage.dao.LeaderDao;
import wiki.zimo.certificate_manage.dao.StudentPrizeInfoDao;
import wiki.zimo.certificate_manage.domain.CertificateType;
import wiki.zimo.certificate_manage.domain.StudentPrizeInfo;
import wiki.zimo.certificate_manage.service.ExcelService;
import wiki.zimo.certificate_manage.service.FileSaveService;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author 子墨
 * @date 2018/10/14 15:38
 * @describe 处理Excel导入导出
 */
@Controller
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private CertificateTypeDao certificateTypeDao;
    @Autowired
    private LeaderDao leaderDao;
    @Autowired
    private StudentPrizeInfoDao studentPrizeInfoDao;

    @Autowired
    private FileSaveService fileSaveUtils;

    @Value("${file.upload-path}")
    private String uploadPath;// 文件保存路径

    @Autowired
    private ExcelService excelService;


    @RequestMapping(value = "/export")
    public void exportExcel(HttpServletResponse response) {
        List<StudentPrizeInfo> list = studentPrizeInfoDao.findAll();
        for (StudentPrizeInfo s:list){
            s.setPrizePhoto(uploadPath + s.getPrizePhoto());
        }
        System.out.println(list.toString());
        // 指定列表标题和工作表名称
        ExportParams params = new ExportParams("证书详细信息","证书详情表");
        try {
            excelService.doExportExcel(response,list,StudentPrizeInfo.class,params,"证书详情表");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping(value = "/import")
    public void importExcel(HttpServletResponse response){
        try {
            List<CertificateType> certificateTypes = excelService.doImportExcel("证书类型.xls",CertificateType.class,new ImportParams());
            for (CertificateType c:certificateTypes){
                System.out.println(c);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
